<?php
/**
 * Created by PhpStorm.
 * User: Jessewang
 * Date: 2017/3/26
 * Time: 下午9:48
 */

namespace App\Service;
use Swoole;
use Swoole\RandomKey;
use App\Libs\UploadApi;

class UploadService extends BaseService
{

    const  CACHE_KEY_LIKE =  "like#";
    const BUKET_NAME = '15s';
    public $upload;
    function __construct()
    {
        $this->upload = Swoole::getInstance()->upload;
    }
    function upload()
    {
        if ($_FILES) {
            //需要生成缩略图
            $this->upload->thumb_width = 136; //缩略图宽度
            $this->upload->thumb_height = 136; //缩略图高度
            $this->upload->thumb_qulitity = 100; //缩略图质量

            //自动压缩图片
            $this->upload->max_width = 600; //约定图片的最大宽度
            $this->upload->max_height = 600; //约定图片的最大高度
            $this->upload->max_qulitity = 90; //图片压缩的质量

            $up_pic = $this->upload->save('Filedata');
            //var_dump($up_pic);
            $up = new UploadApi();
            $up->cos->setRegion('tj');
            $ret = $up->cos->upload(self::BUKET_NAME,$up_pic['thumb_file'], '/head/'.$up_pic['thumb_filename']);
            if($ret['code'] == 0)
            {
                $thumburl = $ret['data']['access_url'];
            }
            else
            {
                $res = array(
                    'errorcode' =>0,
                    'errmsg'=> "上传失败"
                );
                return $res;
            }
            $ret = $up->cos->upload(self::BUKET_NAME,$up_pic['file'], '/head/'.$up_pic['filename']);
            if($ret['code'] == 0)
            {
                $url = $ret['data']['access_url'];
            }
            else
            {
                $res = array(
                    'errorcode' =>0,
                    'errmsg'=> "上传失败"
                );
                return $res;
            }
            $info = array(
                'thumburl' =>$thumburl,
                'url'=>$url
            );
            $res = array(
                'errorcode' =>1,
                'errmsg'=> "上传成功",
                'info'=> $info
            );
            return $res;
        }
    }

}